/**
 * Author: wangxiaoying
 * Email: 1009929414@qq.com
 * Description:时间格式化插件
 */

const FormatDate = {

  //转换为yyyy-dd
  monthFormat(timestamp) {
    timestamp = typeof timestamp === "number" ? timestamp : parseInt(timestamp)
    let time = new Date(timestamp);
    let year = time.getFullYear();
    let month = time.getMonth() + 1;
    return year + '-' + addZero(month);
  },

  //转换为yyyy-dd-mm
  dateFormat(timestamp) {
    timestamp = typeof timestamp === "number" ? timestamp : parseInt(timestamp)
    let time = new Date(timestamp);
    let year = time.getFullYear();
    let month = time.getMonth() + 1;
    let date = time.getDate();
    return year + '-' + addZero(month) + '-' + addZero(date);
  },

  //转换为yyyy-MM-dd HH-mm-ss
  timeFormat(timestamp) {
    timestamp = typeof timestamp === "number" ? timestamp : parseInt(timestamp)
    let time = new Date(timestamp);
    let year = time.getFullYear();
    let month = time.getMonth() + 1;
    let date = time.getDate();
    let hours = time.getHours();
    let minutes = time.getMinutes();
    let seconds = time.getSeconds();
    return year + '-' + addZero(month) + '-' + addZero(date) + ' ' + addZero(hours) + ':' + addZero(minutes) + ':' + addZero(seconds);
  }

}

// 补0
function addZero(m) {
  return m < 10 ? '0' + m : m
}

export default FormatDate


